﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kaleta.Xna.Collision
{
    public class CollisionBinding
    {
        private Dictionary<CollisionGroup, CollisionGroup> CollidingGroups = new Dictionary<CollisionGroup, CollisionGroup>();

        public void Add(CollisionGroup g1, CollisionGroup groups)
        {
            if (!CollidingGroups.ContainsKey(g1))
                CollidingGroups.Add(g1, groups);
            else
                CollidingGroups[g1] |= groups;

            foreach (CollisionGroup group in Enum.GetValues(typeof(CollisionGroup)))
            {
                if (groups.HasFlag(group))
                {
                    if (!CollidingGroups.ContainsKey(group))
                        CollidingGroups.Add(group, g1);
                    else
                        CollidingGroups[group] |= g1;
                }
            }
        }

        public void Remove(CollisionGroup g1, CollisionGroup groups)
        {
            if (CollidingGroups.ContainsKey(g1))
                CollidingGroups[g1] &= ~groups;

            foreach (CollisionGroup group in Enum.GetValues(typeof(CollisionGroup)))
            {
                if (groups.HasFlag(group))
                {
                    if (CollidingGroups.ContainsKey(group))
                        CollidingGroups[group] &= ~g1;
                }
            }
        }

        public bool Collides(CollisionGroup g1, CollisionGroup g2)
        {
            return CollidingGroups[g1].HasFlag(g2);
        }
    }

    
}
